package com.jyh.controller.channel;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.jyh.config.BaseAction;
import com.jyh.config.quantity.Result;
import com.jyh.db.entity.JyhChannelList;
import com.jyh.service.channel.ChannelService;

@RestController
@RequestMapping(value = "/channel")
public class ChannelController extends BaseAction {
	@Autowired
	private ChannelService channelService;
	
	/**
	 * 一级渠道查询
	 * 
	 * @return
	 */
	@RequestMapping(value ="select",method=RequestMethod.GET)
	public ModelAndView selectChannel(HttpServletRequest request,HttpSession session) {
		ModelAndView mv = new ModelAndView("channel/channelSelect");
		String verify = request.getParameter("verify");
		List<JyhChannelList> list = channelService.channelSelect(request, session);
		mv.addObject("channelOne", list);
		mv.addObject("verify", verify);
		return mv;
	}
	
	/**
	 *渠道添加html方法添加
	 * 
	 * @return
	 */
	@RequestMapping(value ="selectAdd",method=RequestMethod.POST)
	@ResponseBody
	public String selectAdd(HttpServletRequest request,String key) {
		String url = request.getContextPath();
		JyhChannelList vo = new JyhChannelList();
		vo.setGeneralizeId(key);
		url = url+"/channel/add";
		String html = channelService.addHtml(url,vo,"新增");
		return html;
	}
	
	/**
	 *渠道修改html方法添加
	 * 
	 * @return
	 */
	@RequestMapping(value ="selectUpdate",method=RequestMethod.POST)
	@ResponseBody
	public String selectUpdate(HttpServletRequest request,String key) {
		String url = request.getContextPath();
		Result html = channelService.selectUpdate(url,key);
		return jsonOut(html);
	}
	
	/**
	 * 下级渠道查询
	 * @param key
	 * @return
	 */
	@RequestMapping(value = "selectLower",method=RequestMethod.POST)
	@ResponseBody
	public String selectLower(HttpServletRequest request,String key) {
		List<JyhChannelList> list = channelService.selectLower(key);
		logger.info("jsonOutSimplify返回数据:{}.",jsonOutSimplify(list));
		return jsonOutSimplify(list);
	}
	
	/**
	 * 渠道简称查询
	 * @param key
	 * @return
	 */
	@RequestMapping(value ="selectAbb",method=RequestMethod.POST)
	@ResponseBody
	public String selectAbb(HttpServletRequest request,String key) {
		List<JyhChannelList> list =channelService.selectAbb(key);
		logger.info("返回数据:{}.",jsonOutSimplify(list));
		return jsonOutSimplify(list);
	}
	
	/**
	 *渠道添加
	 * @param vo
	 */
	@RequestMapping(value ="add")
	public ModelAndView add(JyhChannelList vo,HttpServletRequest request, RedirectAttributes model, HttpSession session ){
		System.out.println("测试");
		String verify = channelService.getAdd(vo);
		model.addAttribute("verify", verify);
		return new ModelAndView("redirect:/channel/select");
	}
	
	/**
	 *渠道修改
	 * @param vo
	 */
	@RequestMapping(value ="update")
	public ModelAndView update(JyhChannelList vo,HttpServletRequest request, RedirectAttributes model, HttpSession session ){
		System.out.println("测试");
		String verify = channelService.getUpdate(vo);
		model.addAttribute("verify", verify);
		return new ModelAndView("redirect:/channel/select");
	}
	
}
